package 行为型模式.命令;

/**
 * @ClassNameCommandTest
 * @Description 调用者->调用（抽象）命名，具体命令->接收者（执行）
 * @Author
 * @Date2022/1/5 10:55
 **/
public class CommandTest {
    public static void main(String[] args) {

        Command powerOn = new PowerOn();
        Command changeChannel = new ChangeChannel();
        RemoteControl remoteControl = new RemoteControl(powerOn);
        remoteControl.call();


        System.out.println("---------------------");
        remoteControl.setCommand(changeChannel);
        remoteControl.call();
    }
}


// 遥控器
class RemoteControl {
    Command command;

    public RemoteControl(Command command) {
        this.command = command;
    }

    public Command getCommand() {
        return command;
    }

    public void setCommand(Command command) {
        this.command = command;
    }

    void call() {
        System.out.println("遥控器收到命令");
        this.command.action();
    }
}

interface Command {
    void action();
}

// 开机
class PowerOn implements Command {
    private Tv tv;

    @Override
    public void action() {
        tv.action();
        System.out.println("开机");
    }

    public PowerOn() {
        tv = new Tv();
    }
}

// 换台
class ChangeChannel implements Command {

    private Tv tv;

    @Override
    public void action() {
        tv.action();
        System.out.println("换台");
    }

    public ChangeChannel() {
        tv = new Tv();
    }
}

class Tv {
    public void action() {
        System.out.println("Tv执行操作...");
    }
}
